package com.runfast.paotui.service.impl;

import com.runfast.common.service.BaseService;
import com.runfast.paotui.dao.mapper.RunfastAgentaccountsMapper;
import com.runfast.paotui.dao.mapper.RunfastAgentbusinessMapper;
import com.runfast.paotui.dao.model.RunfastAgentbusiness;
import com.runfast.paotui.dao.model.RunfastAgentbusinessExample;
import com.runfast.paotui.entity.Region;
import com.runfast.paotui.service.RunfastAgentbusinessService;
import com.runfast.paotui.utils.AmapUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@Service
public class RunfastAgentbusinessServiceImpl extends BaseService<RunfastAgentbusiness, Integer, RunfastAgentbusinessExample> implements RunfastAgentbusinessService {
    @Resource
    private RunfastAgentbusinessMapper dao;
    @Resource
    private RedisTemplate redisTemplate;

    @Override
    public RunfastAgentbusiness getAgentNearBy(Double lng, Double lat) {
        String namespace = "agent:";
        String regionKey = namespace + "region";

        HashOperations hashOperations = redisTemplate.opsForHash();

        Set agentIds = hashOperations.keys(regionKey);
        if (agentIds == null || agentIds.isEmpty()) {
            List<RunfastAgentbusiness> agentbusinesses = this.selectByExample(null);

            for (int i = 0; i < agentbusinesses.size(); i++) {

                RunfastAgentbusiness agentbusiness = agentbusinesses.get(i);

                String province = agentbusiness.getProvince() == null ? "" : agentbusiness.getProvince();
                String city = agentbusiness.getCityName() == null ? "" : agentbusiness.getCityName();
                String district = agentbusiness.getCountyName() == null ? "" : agentbusiness.getCountyName();
                String township = agentbusiness.getArea() == null ? "" : agentbusiness.getArea();

                String address = province + city + district + township;
                List<String> locaions = AmapUtil.getLocaions(address, false);
                String location = locaions.get(0);

                List<Region> regins = AmapUtil.getRegins(location, false);
                Region region = regins.get(0);
                region.setAgentId(agentbusiness.getId());

                hashOperations.put(regionKey, agentbusiness.getId(), region);
            }

        }


        List<Region> regions = AmapUtil.getRegins(lng + "," + lat, false);
        Region userRegion = regions.get(0);
        String userTownship = userRegion.getTownship();
        String userDistrict = userRegion.getDistrict();
        String userCity = userRegion.getCity();

        ArrayList<Region> candidateProvinces = new ArrayList<>();
        ArrayList<Region> candidateCities = new ArrayList<>();
        ArrayList<Region> candidateDistricts = new ArrayList<>();
        ArrayList<Region> candidateTownships = new ArrayList<>();
        for (Object agentId : agentIds) {
            Region region = (Region) hashOperations.get(regionKey, agentId);

            String province = region.getProvince();

            if (StringUtils.isNotBlank(province) && province.equals(province)) {
                candidateProvinces.add(region);
            }
        }

        for (Region region : candidateProvinces) {

            String city = region.getCity();

            if (StringUtils.isNotBlank(city) && city.equals(userCity)) {
                candidateCities.add(region);
            }
        }

        for (Region region : candidateCities) {

            String district = region.getDistrict();

            if (StringUtils.isNotBlank(district) && district.equals(userDistrict)) {
                candidateDistricts.add(region);
            }
        }

        for (Region region : candidateDistricts) {

            String township = region.getTownship();

            if (StringUtils.isNotBlank(township) && township.equals(userTownship)) {
                candidateTownships.add(region);
            }
        }

        if (candidateTownships.isEmpty()) {

            if (candidateDistricts.isEmpty()) {
                if (candidateCities.isEmpty()) {

                    return null;
                } else {
                    return getAgentbusiness(candidateCities);
                }

            } else {
                return getAgentbusiness(candidateDistricts);
            }
        } else {

            return getAgentbusiness(candidateTownships);
        }


    }

    @Override
    public Integer getDefaultAgentId(double longitude, double latitude) {
        return null;
    }

    @Override
    public RunfastAgentbusiness findByid(Integer agentId) {
        return dao.findByid(agentId);
    }

    private RunfastAgentbusiness getAgentbusiness(ArrayList<Region> candidateRegions) {
        Region region = candidateRegions.get(0);
        Integer agentId = region.getAgentId();
        return this.selectByPrimaryKey(agentId);
    }
}